package com.regent.central.bill.service.bean.mrp;

import com.regent.central.bill.common.BillPurchaseArriveStatus;
import com.regent.central.bill.common.DataChangeType;
import com.regent.central.bill.core.mrp.ActionMaterialPurchase;
import com.regent.central.bill.core.mrp.BillMaterialPurchaseSku;
import com.regent.central.bill.repository.mrp.BillMaterialPurchaseSkuMapper;
import com.regent.central.bill.service.mrp.BillMaterialPurchaseSkuService;
import com.regent.central.infrastructure.common.Uid;
import com.regent.central.infrastructure.service.bean.DomainServiceBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Set;

/**
 * @author 钟宝林
 * @date 2018-06-23 13:10
 **/
@Service
public class BillMaterialPurchaseSkuServiceBean extends DomainServiceBean<BillMaterialPurchaseSkuMapper, BillMaterialPurchaseSku> implements BillMaterialPurchaseSkuService {

    @Resource
    private BillMaterialPurchaseSkuMapper billMaterialPurchaseSkuMapper;

    @Transactional
    @Override
    public void saveByActionPurchase(String billMaterialPurchaseId, Set<ActionMaterialPurchase> actionMaterialPurchases) {
        if (StringUtils.isBlank(billMaterialPurchaseId) || actionMaterialPurchases == null || actionMaterialPurchases.size() <= 0) {
            return;
        }
        for (ActionMaterialPurchase actionMaterialPurchase : actionMaterialPurchases) {
            BillMaterialPurchaseSku billMaterialPurchaseSku = new BillMaterialPurchaseSku();
            billMaterialPurchaseSku.setId(Uid.random());
            billMaterialPurchaseSku.setBillMaterialPurchaseId(billMaterialPurchaseId);
            billMaterialPurchaseSku.setFacMissionId(actionMaterialPurchase.getFacMissionId());
            billMaterialPurchaseSku.setCommonMaterialsSkuId(actionMaterialPurchase.getCommonMaterialsSkuId());
            billMaterialPurchaseSku.setQuantityPurchase(actionMaterialPurchase.getQuantity());
            billMaterialPurchaseSkuMapper.insertSelective(billMaterialPurchaseSku);
        }
    }

    @Transactional
    @Override
    public int setArriveStatus(BillPurchaseArriveStatus arriveStatus, String billMaterialPurchaseId, String facMissionId, String materialsSkuId, DataChangeType dataChangeType) {
        return billMaterialPurchaseSkuMapper.setArriveStatus(arriveStatus.getValue(), billMaterialPurchaseId, facMissionId, materialsSkuId, dataChangeType.getValue());
    }

    @Transactional
    @Override
    public int setQuantityArrive(BigDecimal quantityArrive, String billMaterialPurchaseId, String facMissionId, String materialsSkuId) {
        return billMaterialPurchaseSkuMapper.setQuantityArrive(quantityArrive, billMaterialPurchaseId, facMissionId, materialsSkuId);
    }

    @Transactional
    @Override
    public int setArriveStatusSystem(BillPurchaseArriveStatus arriveStatus, String billMaterialPurchaseId, String facMissionId, String materialsSkuId) {
        return billMaterialPurchaseSkuMapper.setArriveStatusSystem(arriveStatus.getValue(), billMaterialPurchaseId, facMissionId, materialsSkuId);
    }
}
